<?php

namespace App\Repositories\Ca;

use App\Facade\Code;
use App\Models\BaseModel;
use App\Models\Ca\CustomerLog;
use App\Repositories\BaseRepository;
use App\Repositories\System\AreaRepository;
use App\Repositories\System\UserRepository;
use Illuminate\Support\Arr;

class CustomerLogRepository extends BaseRepository
{

    const FIELD_MAP = [
        'id' => '预约号',

        'consultation_type' => '咨询类型',
        'name' => '挂号人姓名',
        'age' => '年龄',
        'real_name' => '真实姓名',
        'hf_time' => '下次回访时间',
        'gender' => '性别',
        'mobile' => '联系电话',
        'weixin' => '微信号',
        'qq' => 'qq号',
        'phone' => '座机',
        'yy_time' => '预约时间',
        'source_id' => '来源媒体',
        'consultation_way_id' => '咨询方式',
        'medical_id' => '话务员',
        'yy_doctor_id' => '预约医生',
        'province_id' => '所属省份',
        'city_id' => '所属市区',
        'address' => '地址',
        'custom_service_id' => '咨询客服',
        'fb_time' => '发病时间',
        'cost_money' => '费用',
        'source_site' => '来源网站',
        'source_ip' => '来源IP',
        'source_entry' => '来源入口',
        'source_consulting_page' => '咨询页面',
        'source_search_keywords' => '搜索关键词',
        'source_matching_keywords' => '匹配关键词',
        'gm_words' => '购买词',
        'creator_id' => '创建人用户编号',
        'appointment_loss_reason' => '预约流失原因',
        'arrived_loss_reason' => '到院流失原因',
        'medical_history' => '就诊史',
        'disease_desc' => '病情描述',
        'swt' => '商务通内容',
        'remark' => '其他备注',
        'disease_data' => '疾病数据',
        'visit_data' => '回访数据',
        'jz_doctor_id' => '就诊医生',
        'jz_guide_doctor_id' => '接待导医',
        'stage' => '接待导医',
        'hospital_id' => '医院',
        'updated_at' => '更新时间',
        'created_at' => '新增时间',
        'status' => '状态',
        'is_work_today' => '是否今日工作',
        'is_public' => '是否加入公海',
        'last_visit_time' => '最后回访时间',
        'last_visit_user_id' => '最后回访用户ID',
        'jz_time' => '预约日期',
        'is_spend' => '是否消费',
        'spend_time' => '消费时间',
        'tady_yy_time' => '预约日期',
    ];

    const EXCEPT_FIELD = [
        'real_name',
        'qq',
        'phone',
        'address',
        'consultation_way_id',
        'fb_time',
        'medical_history',
        'remark',
    ];

    public function logs($id)
    {
        $logs = CustomerLog::query()
            ->leftJoin('system__users', 'system__users.id', '=', 'ca__customer_logs.user_id')
            ->where('ca__customer_logs.customer_id', '=', $id)
            ->select(
                'ca__customer_logs.id',
                'system__users.name',
                'system__users.mobile',
                'system__users.username',
                'ca__customer_logs.desc',
                'ca__customer_logs.type',
                'ca__customer_logs.change_data',
                'ca__customer_logs.created_at'
            )->orderByDesc('id')
            ->get();
        foreach ($logs as $log) {

            $log['type_text'] = Arr::get(CustomerLog::TYPE_MAP, $log['type'], '');
            $old = Arr::get($log['change_data'], 'old', []);
            $new = Arr::get($log['change_data'], 'new', []);
            $fields = [];
            foreach ($old as $field => $value) {
                if (in_array($field, self::EXCEPT_FIELD)) continue;
                $old_value = Arr::get($old, $field, null);
                $new_value = Arr::get($new, $field, null);
                if ($old_value != $new_value) {
                    $fields[] = [
                        'field' => $field,
                        'field_name' => Arr::get(self::FIELD_MAP, $field, $field),
                        'old_value' => $old_value,
                        'new_value' => $new_value,

                        'old_value_text' => $this->getFieldValueText($old_value, $field),
                        'new_value_text' => $this->getFieldValueText($new_value, $field),
                    ];
                }
                unset($old[$field], $new[$field]);
            }

            foreach ($new as $field => $value) {
                if (in_array($field, self::EXCEPT_FIELD)) continue;
                $old_value = Arr::get($old, $field, null);
                $new_value = Arr::get($new, $field, null);
                if ($old_value != $new_value) {
                    $fields[] = [
                        'field' => $field,
                        'field_name' => Arr::get(self::FIELD_MAP, $field, $field),
                        'old_value' => $old_value,
                        'new_value' => $new_value,

                        'old_value_text' => $this->getFieldValueText($old_value, $field),
                        'new_value_text' => $this->getFieldValueText($new_value, $field),
                    ];
                }
                unset($old[$field], $new[$field]);
            }
            $log['change_data'] = $fields;
        }
        return $logs;
    }


    function getFieldValueText($value, $filed)
    {
        if (is_array($value)) {
            return json_encode($value, JSON_UNESCAPED_UNICODE);
        }
        if ($filed == 'consultation_type') {
            return Code::text("AB", $value);
        } elseif ($filed == 'gender') {
            return Code::text("AA", $value);
        } elseif ($filed == 'consultation_type') {
            return Code::text("AA", $value);
        } elseif ($filed == 'status') {
            return Code::text("AC", $value);
        } elseif ($filed == 'consultation_way') {
            $rep = new ConsultationWayRepository();
            return $rep->getName($value);
        } elseif ($filed == 'yy_docker_id') {
            $rep = new DockerRepository();
            return $rep->getName($value);
        } elseif ($filed == 'source_id') {
            $rep = new SourceRepository();
            return $rep->getName($value);
        } elseif ($filed == 'medical_id') {
            $rep = new UserRepository();
            return $rep->getName($value);
        } elseif ($filed == 'custom_service_id') {
            $rep = new UserRepository();
            return $rep->getName($value);
        } elseif ($filed == 'province_id') {
            $rep = new AreaRepository();
            return $rep->getName($value);
        } elseif ($filed == 'city_id') {
            $rep = new AreaRepository();
            return $rep->getName($value);
        }


        return $value;
    }

    function getModel(): BaseModel
    {
        return new CustomerLog();
    }
}